package cpnt.iris
{
	import define.SlotConst;

	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;

	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.effects.Glow;

	public class Slot extends Canvas
	{
		private var iconSlot:Image;
		private var iconArmory:Image;

		private var mouseOverEffect:Glow;

		public function Slot()
		{
			super();
			// 
			this.height = 34;
			this.width = 34;
			// x: 0,y: 0, 34 * 34 
			this.iconSlot = new Image();
			this.iconSlot.addEventListener(Event.COMPLETE, iconSlotLoaded);
			this.iconSlot.addEventListener(IOErrorEvent.IO_ERROR, iconSlot_ieErrorHandler);
			this.iconSlot.addEventListener(MouseEvent.ROLL_OVER, iconSlotMouseOver);
			this.iconSlot.addEventListener(MouseEvent.ROLL_OUT, iconSlotMouseOut);
			this.iconSlot.buttonMode = true;
			// x: 2,y: 2, 32 * 32
			this.iconArmory = new Image();
			this.iconArmory.addEventListener(Event.COMPLETE, iconArmoryLoaded);
			// Effect
			this.mouseOverEffect = new Glow(this.iconSlot);
			this.mouseOverEffect.duration = 300;
			this.mouseOverEffect.alphaFrom = 1.0;
			this.mouseOverEffect.alphaTo = 0.7;
			this.mouseOverEffect.blurXFrom = 0;
			this.mouseOverEffect.blurXTo = 12;
			this.mouseOverEffect.blurYFrom = 0;
			this.mouseOverEffect.blurYTo = 12;
			this.mouseOverEffect.color = 0x0099FF;
			//this.mouseOverEffect.color = 0x22A050;
			// Event
			this.addEventListener(MouseEvent.CLICK, clickListener);
		}

		private function iconSlot_ieErrorHandler(event:IOErrorEvent):void
		{
		}

		private function iconSlotLoaded(event:Event):void
		{
			this.addChild(event.target as DisplayObject);
		}

		private function iconSlotMouseOver(event:MouseEvent):void
		{
			this.mouseOverEffect.play();
		}

		private function iconSlotMouseOut(event:MouseEvent):void
		{
			this.mouseOverEffect.play(null, true);
		}

		private function iconArmoryLoaded(event:Event):void
		{
			this.addChild(event.target as DisplayObject);
		}
		private var _slot:String;

		public function set slot(value:String):void
		{
			this._slot = value;

			this.iconSlot.load(SlotConst.getIconUrl(this._slot));
		}

		public function getSlots():Array
		{
			return SlotConst.getSlots(this._slot);
		}

		private function clickListener(event:MouseEvent):void
		{
			this.dispatchEvent(new SlotEvent(SlotEvent.SLOT_EVENT_CLICK));
		}
	}
}